package org.classg.edu.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.classg.edu.client.OrderClient;
import org.classg.edu.entity.Course;
import org.classg.edu.service.ChapterService;
import org.classg.edu.service.CourseService;
import org.classg.edu.vo.ChapterVo;
import org.classg.edu.vo.front.CourseInfoFrontVo;
import org.classg.edu.vo.front.CourseQueryFrontVo;
import org.classg.utils.JwtUtils;
import org.classg.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Api(description = "课程前台管理")
@RestController
@RequestMapping("/eduservice/coursefront")
@CrossOrigin
public class CourseFrontController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private ChapterService chapterService;

    @Autowired
    private OrderClient orderClient;

    @ApiOperation("查询所有课程分页数据包含查询条件")
    @PostMapping("/queryAllCoursePage/{current}/{limit}")
    public R queryAllCoursePage(@ApiParam(name = "current", value = "当前页", required = true) @PathVariable Long current,
                                @ApiParam(name = "limit", value = "每页显示记录数", required = true) @PathVariable Long limit,
                                @ApiParam(name = "courseQueryFrontVo", value = "查询前台页面条件对象", required = false) @RequestBody CourseQueryFrontVo courseQueryFrontVo) {
        Page<Course> page = new Page<>(current, limit);
        Map<String, Object> map = courseService.queryAllCoursePage(page, courseQueryFrontVo);
        return R.ok().data(map);
    }

    @ApiOperation("根据课程id查询课程详情信息")
    @GetMapping("/queryCourseInfoVoByCourseId/{id}")
    public R queryCourseInfoVoByCourseId(@ApiParam(name = "id", value = "课程id", required = true) @PathVariable String id, HttpServletRequest request) {
        // 根据课程id查询课程详情信息
        CourseInfoFrontVo courseInfoVo = courseService.queryCourseInfoVoByCourseId(id);
        // 根据课程id查询章节和小节
        List<ChapterVo> chaptersAndVideos = chapterService.queryChapterAndVideoByCourseId(id);
        // 远程调用，判断是否购买
        String userId = JwtUtils.getMemberIdByJwtToken(request);
        Boolean isBuy = false;
        if (!StringUtils.isEmpty(userId)) {
            isBuy = orderClient.queryOrderByUserIdAndCourseId(userId, id);
        }
        return R.ok().data("courseInfoVo", courseInfoVo).data("chaptersAndVideos", chaptersAndVideos).data("isBuy", isBuy);
    }

    @GetMapping("/test/{id}")
    public R test(@ApiParam(name = "id", value = "课程id", required = true) @PathVariable String id, HttpServletRequest request) {
        // 根据课程id查询课程详情信息
        CourseInfoFrontVo courseInfoVo = courseService.queryCourseInfoVoByCourseId(id);
        // 根据课程id查询章节和小节
        List<ChapterVo> chaptersAndVideos = chapterService.queryChapterAndVideoByCourseId(id);
        Boolean isBuy = orderClient.queryOrderByUserIdAndCourseId(JwtUtils.getMemberIdByJwtToken(request), id);
        return R.ok().data("courseInfoVo", courseInfoVo).data("chaptersAndVideos", chaptersAndVideos).data("isBuy", isBuy);
    }

}
